package mspdt.ubzuuch.mn;

import java.util.ArrayList;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MainExpandableAdapter extends BaseExpandableListAdapter {
	 
	  private Context myContext;
	  private ArrayList<String> parent_datas;
	  private ArrayList<Integer> parent_counts;
	  private ArrayList<ArrayList<String>> child_datas;
	  private ArrayList<ArrayList<Integer>> child_counts;
	  public MainExpandableAdapter(Context context, ArrayList<String> parent_data, ArrayList<Integer> parent_count,
			  ArrayList<ArrayList<String>> child_data, ArrayList<ArrayList<Integer>> child_count) {
	   myContext = context;
	   this.parent_datas=parent_data;
	   this.child_datas=child_data;
	   this.parent_counts=parent_count;
	   this.child_counts=child_count;
	  }
	  
	  @Override
	  public Object getChild(int groupPosition, int childPosition) {
	   return null;
	  }
	 
	  @Override
	  public long getChildId(int groupPosition, int childPosition) {
	   return 0;
	  }
	 
	  @Override
	  public View getChildView(int groupPosition, int childPosition,
	    boolean isLastChild, View convertView, ViewGroup parent) {
	    
	   if (convertView == null) {
	    LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    convertView = inflater.inflate(R.layout.main_exp_child, null);
	   }
	   
	   if(childPosition%2==0)
		   convertView.setBackgroundColor(myContext.getResources().getColor(R.color.white));
	   else
		   convertView.setBackgroundColor(myContext.getResources().getColor(R.color.expandable_child));
	   	TextView child_name = (TextView) convertView.findViewById(R.id.main_child_name);
	   	TextView child_count = (TextView) convertView.findViewById(R.id.main_child_counter);
	    child_name.setText(Html.fromHtml(child_datas.get(groupPosition).get(childPosition)));
	    if(child_counts!=null)
	    child_count.setText("("+child_counts.get(groupPosition).get(childPosition)+")");
	   return convertView;
	  }
	 
	  @Override
	  public int getChildrenCount(int groupPosition) {
	   return child_datas.get(groupPosition).size();
	  }
	 
	  @Override
	  public Object getGroup(int groupPosition) {
	   return null;
	  }
	 
	  @Override
	  public int getGroupCount() {
	   return parent_datas.size();
	  }
	 
	  @Override
	  public long getGroupId(int groupPosition) {
	   return 0;
	  }
	 
	  @Override
	  public View getGroupView(int groupPosition, boolean isExpanded,
	    View convertView, ViewGroup parent) {
	 
	   if (convertView == null) {
	    LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    convertView = inflater.inflate(R.layout.main_exp_group, null);
	   }
	    
	   TextView group_name = (TextView)convertView.findViewById(R.id.main_group_name);
	   TextView group_count = (TextView)convertView.findViewById(R.id.main_group_count);
	   group_name.setText(parent_datas.get(groupPosition));
	   if(parent_counts!=null)
	   group_count.setText("("+parent_counts.get(groupPosition)+")");
	    
	   return convertView;
	  }
	 
	  @Override
	  public boolean hasStableIds() {
	   return false;
	  }
	 
	  @Override
	  public boolean isChildSelectable(int groupPosition, int childPosition) {
	   return true;
	  }
	 }